package com.mall.controller;

import com.mall.dto.ActionResult;
import com.mall.po.Brand;
import com.mall.service.BrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author XueWanglin
 * @ClassName BrandController
 * @Description TODO
 * @Date 2021/1/5 20:38
 * @Version 1.0
 */
@Api(tags = "品牌相关操作")
@RestController
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 查询品牌列表
     * @return
     */
    @ApiOperation(value = "分页获取品牌信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "查询的页码",required = true ),
            @ApiImplicitParam(name = "pagesize",value = "每一页包含数据条数",required = true)
    })
    @RequestMapping(value = "shangcheng/brandlist",method = RequestMethod.GET)
    public ActionResult getBrandList(Integer page, Integer pagesize){
//        System.out.println(page);
        List<Brand> list = brandService.getBrandList(page,pagesize);
//        System.out.println(list);
        if (list!=null){
            return new ActionResult(200,"ok",list);
        }else {
            return new ActionResult(404,"找不到数据",null);
        }
    }

    /**
     * 查询一个品牌信息
     * @return
     */
    @ApiOperation(value = "查询单个品牌名称")
    @ApiImplicitParam(name = "brandname",value = "品牌名称",required = true,dataType = "String")
    @RequestMapping(value = "shangcheng/brandquery",method = RequestMethod.GET)
    public ActionResult getOneByBrandname(String brandname){
//        System.out.println(goods.getGoodsname());
        Brand brand = brandService.getOneByBrandname(brandname);
        if (brand!=null){
            return new ActionResult(200,"success",brand);
        }else {
            return new ActionResult(404,"fail",null);
        }
    }

    /**
     * 通过对象添加品牌信息
     * @return
     */
    @ApiOperation(value = "添加品牌信息")
    @ApiImplicitParam(name = "brand",value = "没有品牌id的对象（json）",required = true)
    @RequestMapping(value = "shangcheng/brandadd",method = RequestMethod.POST)
    public ActionResult addBrandByExample(@RequestBody Brand brand){
        Brand brand1 = brandService.addBrandByExample(brand);
        if (brand1!=null){
            return new ActionResult(200,"success",brand1);
        }else {
            return new ActionResult(404,"fail",null);
        }
    }

    /**
     * 通过id删除品牌信息
     * @return
     */
    @ApiOperation(value = "删除品牌")
    @ApiImplicitParam(name = "id",value = "要删除目标品牌的id",required = true)
    @RequestMapping(value = "shangcheng/branddel",method = RequestMethod.DELETE)
    public ActionResult delBrandById(int id){
        boolean result = brandService.delBrandById(id);
        if (result){
            return new ActionResult(200,"success",null);
        }else {
            return new ActionResult(404,"fail",null);
        }
    }

    /**
     * 通过对象更改品牌信息
     * @return
     */
    @ApiOperation(value = "更新品牌内容")
    @ApiImplicitParam(name = "brand",value = "更改后的内容（不能更改品牌id）",required = true)
    @RequestMapping(value = "shangcheng/brandupdate",method = RequestMethod.PUT)
    public ActionResult updateBrandByExample(@RequestBody Brand brand){
        boolean result = brandService.updateBrandByExample(brand);
        if (result){
            return new ActionResult(200,"success",null);
        }else {
            return new ActionResult(404,"fail",null);
        }
    }

    /**
     * 批量删除品牌
     * @param ids
     * @return
     */
    @ApiOperation(value = "批量删除品牌")
    @ApiImplicitParam(name = "ids",value = "要删除的品牌id数组",required = true)
    @RequestMapping(value = "shangcheng/brandlistdel",method = RequestMethod.DELETE)
    public ActionResult delBrandListById(@RequestParam("ids") Integer[] ids){
        boolean result = brandService.delBrandListById(ids);
        if (result){
            return new ActionResult(200,"success",null);
        }else {
            return new ActionResult(404,"fail",null);
        }
    }
}

